package com.njit.WeStudy.dao;

import com.njit.WeStudy.dataobject.Invitation;
import java.util.List;

import com.njit.WeStudy.dataobject.InvitationView;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

public interface InvitationMapper {
    @Delete({
        "delete from invitation",
        "where invitation_id = #{invitationId,jdbcType=INTEGER}"
    })
    int deleteByPrimaryKey(Integer id);

    @Insert({
        "insert into invitation (user_id, ",
        "title, preview_image_url, ",
        "create_time, update_time, ",
        "delete_time, like_number, ",
        "star_number, comment_number,html_url)",
        "values ( #{userId,jdbcType=INTEGER}, ",
        "#{title,jdbcType=VARCHAR}, #{previewImageUrl,jdbcType=VARCHAR}, ",
        "now(), #{updateTime,jdbcType=TIMESTAMP}, ",
        "#{deleteTime,jdbcType=TIMESTAMP}, 0, ",
        "0, 0,#{htmlUrl,jdbcType=VARCHAR})"
    })
    int insert(Invitation record);

    @Select({
        "select",
        "invitation_id, user_id, title, preview_image_url, create_time, update_time, delete_time, ",
        "like_number, star_number, comment_number,html_url",
        "from invitation",
        "where invitation_id = #{invitationId,jdbcType=INTEGER}"
    })
    @Results(id = "resultMap",value = {
        @Result(column="invitation_id", property="invitationId", jdbcType=JdbcType.INTEGER, id=true),
        @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
        @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
        @Result(column="preview_image_url", property="previewImageUrl", jdbcType=JdbcType.VARCHAR),
        @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="delete_time", property="deleteTime", jdbcType=JdbcType.TIMESTAMP),
        @Result(column="like_number", property="likeNumber", jdbcType=JdbcType.INTEGER),
        @Result(column="star_number", property="starNumber", jdbcType=JdbcType.INTEGER),
        @Result(column="comment_number", property="commentNumber", jdbcType=JdbcType.INTEGER),
        @Result(column="html_url", property="htmlUrl", jdbcType=JdbcType.VARCHAR)
    })
    Invitation selectByPrimaryKey(Integer invitationId);

    @Select({
            "select distinct ",
            "invitation_id, invitation.user_id, title, preview_image_url, create_time, update_time, delete_time, ",
            "like_number, star_number, comment_number,html_url,user_name,head_image",
            "from invitation,user where invitation.user_id = user.user_id and invitation_id = #{invitationId,jdbcType=INTEGER}"
    })
    @ResultMap("resultMapOfView")
    //用于查询invitationView的某条记录
    InvitationView selectViewByPrimaryKey(Integer invitationId);


    @Select({
        "select",
        "invitation_id, user_id, title, preview_image_url, create_time, update_time, delete_time, ",
        "like_number, star_number, comment_number,html_url",
        "from invitation order by create_time desc"
    })
    @ResultMap("resultMap")
    List<Invitation> selectAll();


    //查询和帖子作者相关的信息
    @Select({
            "select distinct ",
            "invitation_id, invitation.user_id, title, preview_image_url, create_time, update_time, delete_time, ",
            "like_number, star_number, comment_number,html_url,user_name,head_image",
            "from invitation,user where invitation.user_id = user.user_id  order by create_time desc"
    })
    @Results(id = "resultMapOfView",value = {
            @Result(column="invitation_id", property="invitationId", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="user_id", property="userId", jdbcType=JdbcType.INTEGER),
            @Result(column="title", property="title", jdbcType=JdbcType.VARCHAR),
            @Result(column="preview_image_url", property="previewImageUrl", jdbcType=JdbcType.VARCHAR),
            @Result(column="create_time", property="createTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="update_time", property="updateTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="delete_time", property="deleteTime", jdbcType=JdbcType.TIMESTAMP),
            @Result(column="like_number", property="likeNumber", jdbcType=JdbcType.INTEGER),
            @Result(column="star_number", property="starNumber", jdbcType=JdbcType.INTEGER),
            @Result(column="comment_number", property="commentNumber", jdbcType=JdbcType.INTEGER),
            @Result(column="html_url", property="htmlUrl", jdbcType=JdbcType.VARCHAR),
            @Result(column="user_name", property="userName", jdbcType=JdbcType.VARCHAR),
            @Result(column="head_image", property="headImage", jdbcType=JdbcType.VARCHAR),
    })
    //这边list里面是InvitationView而不是Invitation了
    List<InvitationView> selectAllView();

    @Update({
        "update invitation",
        "set user_id = #{userId,jdbcType=INTEGER},",
          "title = #{title,jdbcType=VARCHAR},",
          "preview_image_url = #{previewImageUrl,jdbcType=VARCHAR},",
          "update_time = #{updateTime,jdbcType=TIMESTAMP},",
          "delete_time = #{deleteTime,jdbcType=TIMESTAMP},",
          "like_number = #{likeNumber,jdbcType=INTEGER},",
          "star_number = #{starNumber,jdbcType=INTEGER},",
          "comment_number = #{commentNumber,jdbcType=INTEGER}",
            "html_url = #{htmlUrl,jdbcType=VARCHAR}",
        "where invitation_id = #{invitationId,jdbcType=INTEGER}"
    })
    int updateByPrimaryKey(Invitation record);

    @UpdateProvider(type = UpdateBuilder.class,method = "updateInvitationNum")
    int updateNumById(@Param(value = "info") String info,@Param(value = "num") int  num,@Param(value = "id") int id);
}